﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace Tanmia.Basis.Data.NHibernate
{
    public class SessionManager : ISessionManager, ISessionGate
    {
        #region Private Fields
        private DatabaseConfiguration dbConfig;
        private ISession session;
        private IStatelessSession statelessSession;
        #endregion

        public SessionManager(DatabaseConfiguration dbConfig)
        {
            this.dbConfig = dbConfig;
        }

        #region ISessionManager Members

        public void Close()
        {
            if (session != null)
                session.Dispose();

            if (statelessSession != null)
                statelessSession.Dispose();

            session = null;
            statelessSession = null;
        }

        #endregion

        #region ISessionGate Members

        public ISession Session
        {
            get
            {
                if (session == null)
                    session = dbConfig.SessionFactory.OpenSession();
                return session;
            }
        }

        public IStatelessSession StatelessSesion
        {
            get
            {
                if (statelessSession != null)
                    statelessSession = dbConfig.SessionFactory.OpenStatelessSession();
                return statelessSession;
            }
        }

        #endregion
    }
}
